﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace CMS.Common.Validation
{
    public enum RelationshipType
    {
        /// <summary>
        /// GreaterThan
        /// </summary>
        GreaterThan,

        /// <summary>
        /// GreaterThanOrEqual
        /// </summary>
        GreaterThanOrEqual,

        /// <summary>
        /// LessThan
        /// </summary>
        LessThan,

        /// <summary>
        /// LessThanOrEqual
        /// </summary>
        LessThanOrEqual
    }

    public enum PropertyValueType
    {
        /// <summary>
        /// DateTime
        /// </summary>
        DateTime,

        /// <summary>
        /// Number
        /// </summary>
        Number,
        
        /// <summary>
        /// Decimal
        /// </summary>
        Decimal
    }

    public class RelationshipWithAttribute : ValidationAttribute, IClientValidatable
    {
        public string Property { get; set; }

        public PropertyValueType PropertyValueType { get; set; }

        public RelationshipType RelationshipType { get; set; }

        public object Value { get; set; }

        public override bool IsValid(object value)
        {
            return true;
            ////if (value == null)
            ////{
            ////    return true;
            ////}

            ////object value2;
            ////if (!string.IsNullOrEmpty(Property))
            ////{
            ////    value2 = HttpContext.Current.Request[Property];
            ////}
            ////else
            ////{
            ////    value2 = Value;
            ////}            

            ////switch (PropertyValueType)
            ////{
            ////    case Validation.PropertyValueType.DateTime:
            ////        DateTime temp;
            ////        if (!(DateTime.TryParse(value.ToString(), out temp) && DateTime.TryParse(value2.ToString(), out temp)))
            ////        {
            ////            return true;
            ////        }

            ////        switch (RelationshipType)
            ////        {
            ////            case Validation.RelationshipType.GreaterThan:
            ////                return Convert.ToDateTime(value).Date > Convert.ToDateTime(value2).Date;
            ////            case Validation.RelationshipType.GreaterThanOrEqual:
            ////                return Convert.ToDateTime(value).Date >= Convert.ToDateTime(value2).Date;
            ////            case Validation.RelationshipType.LessThan:
            ////                return Convert.ToDateTime(value).Date < Convert.ToDateTime(value2).Date;
            ////            case Validation.RelationshipType.LessThanOrEqual:
            ////                return Convert.ToDateTime(value).Date <= Convert.ToDateTime(value2).Date;
            ////        }

            ////        break;
            ////    case Validation.PropertyValueType.Number:
            ////        switch (RelationshipType)
            ////        {
            ////            case Validation.RelationshipType.GreaterThan:
            ////                return Convert.ToDouble(value) > Convert.ToDouble(value2);
            ////            case Validation.RelationshipType.GreaterThanOrEqual:
            ////                return Convert.ToDouble(value) >= Convert.ToDouble(value2);
            ////            case Validation.RelationshipType.LessThan:
            ////                return Convert.ToDouble(value) < Convert.ToDouble(value2);
            ////            case Validation.RelationshipType.LessThanOrEqual:
            ////                return Convert.ToDouble(value) <= Convert.ToDouble(value2);
            ////        }

            ////        break;
            ////    case Validation.PropertyValueType.Decimal:
            ////        switch (RelationshipType)
            ////        {
            ////            case Validation.RelationshipType.GreaterThan:
            ////                return Convert.ToDecimal(value) > Convert.ToDecimal(value2);
            ////            case Validation.RelationshipType.GreaterThanOrEqual:
            ////                return Convert.ToDecimal(value) >= Convert.ToDecimal(value2);
            ////            case Validation.RelationshipType.LessThan:
            ////                return Convert.ToDecimal(value) < Convert.ToDecimal(value2);
            ////            case Validation.RelationshipType.LessThanOrEqual:
            ////                return Convert.ToDecimal(value) <= Convert.ToDecimal(value2);
            ////        }

            ////        break;
            ////}

            ////return true;
        }

        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var rule = new ModelClientValidationRule
            {
                ValidationType = "relationshipwith",
                ErrorMessage = FormatErrorMessage(metadata.GetDisplayName())
            };

            if (Value != null)
            {
                rule.ValidationParameters["othervalue"] = Value;
            }
            else
            {
                rule.ValidationParameters["othervalue"] = string.Empty;
            }

            if (Property != null)
            {
                rule.ValidationParameters["otherproperty"] = Property;
            }
            else
            {
                rule.ValidationParameters["otherproperty"] = string.Empty;
            }

            rule.ValidationParameters["relationshiptype"] = RelationshipType.ToString();
            rule.ValidationParameters["propertyvaluetype"] = PropertyValueType.ToString();
            yield return rule;
        }
    }
}